package com.foo.aop.Aspect;

import java.util.Arrays;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class UserProfiler 
{
	@Pointcut("execution(* com.foo.DataProvider.*.*(..))")
    public void inceptorMethods(){}
	
	private final Log log = LogFactory.getLog(this.getClass());
	

	@Around("com.foo.aop.Aspect.SystemArchitecture.dataAccessOperation()")
	public Object profile(ProceedingJoinPoint proceedingJP) throws Throwable
	{
		StringBuilder logMessage = new StringBuilder();
		Object output = null;
		logMessage.append("\n Class Name: " + proceedingJP.getTarget().getClass().getName());
		logMessage.append("\n Method Name: " + proceedingJP.getSignature().getName());
		logMessage.append("\n Argument: " + Arrays.toString(proceedingJP.getArgs()));
		long start = System.currentTimeMillis();
        try
        {
        	output = proceedingJP.proceed();
        }
        catch(Exception e)
        {
        	logMessage.append("\n Exception Occured!!Exception Message is: " + e.getMessage());
        }
        long elapsedTime = System.currentTimeMillis() - start;
        logMessage.append("\n Method execution time: " + elapsedTime + " ms.");
        log.info(logMessage.toString());
        return output;
	}

}
